﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace MUD.Data
{
    public static class Provider
    {
        public static string ConnectionString;
        public static event EventHandler<LogEventArgs> OnLog;

        static Provider()
        {
            
        }

        public static DataSet ExecuteSproc(string procedureName, Dictionary<string, string> parameters)
        {
            SqlCommand cmd = new SqlCommand(procedureName);
            cmd.CommandType = CommandType.StoredProcedure;
            foreach (string parameterName in parameters.Keys)
            {
                cmd.Parameters.AddWithValue(parameterName, parameters[parameterName]);
            }
            SqlConnection conn = new SqlConnection(ConnectionString);

            try
            {
                cmd.Connection = conn;
                SqlDataAdapter da = new SqlDataAdapter(cmd);

                DataSet retval = new DataSet();
                da.Fill(retval);
                return retval;
            }
            catch (Exception ex)
            {
                Log(ex.Message);
            }
            finally
            {
                conn.Close();
                cmd.Dispose();
            }
            return null;
        }

        private static void Log(string message)
        {
            if (OnLog != null)
                OnLog(null, new LogEventArgs(message));
        }
    }
}
